<template>
  <div>
  <el-dialog title="分配角色" :visible.sync="dialogVisible" :destroy-on-close="false" :append-to-body="true" :close-on-click-modal="false">
    <el-checkbox-group v-model="selected">
      <el-checkbox :key="role.id" v-for="role in roles" :label="role.id">{{role.roleName}}</el-checkbox>
    </el-checkbox-group>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addUserRole">确 定</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
import {addUserRole} from '@/api/user'
export default {
  name: "UserRoleDialog",
  data() {
    return {
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.$store.state.user.dialogVisible;
      },
      set(value) {
        this.$store.state.user.dialogVisible = value;
      }
    },
    roles: {
      get() {
        return this.$store.state.user.roles;
      },
      set(value) {
        this.$store.state.user.roles = value;
      }
    },
    selected: {
      get() {
        return this.$store.state.user.selected;
      },
      set(value) {
        this.$store.state.user.selected = value;
      }
    }
  },
  methods: {
    addUserRole() {
      var params = {
        id: this.$store.state.user.hasRoles === null ? null : this.$store.state.user.hasRoles.id,
        userId: this.$store.state.user.userId,
        roleIds: this.selected
      }
      addUserRole(params).then((res) => {
        this.$message.success(res.data.msg);
        this.dialogVisible = false;
      })
    }
  }
}
</script>

<style scoped>

</style>